package com.a16.yuejian.yuejian.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.javabean.User;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.listener.UserListener;
import com.a16.yuejian.yuejian.model.UserModel;
import com.a16.yuejian.yuejian.roundimagevIew.RoundImageView;
import com.squareup.picasso.Picasso;


public class MyMessageActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerView recyclerview;
    private RecyclerView.LayoutManager mlayoutManager;
    private ImageView my_message_back,my_message_right,my_message_replace;
    private TextView my_message_username,my_message_phone;
    private RoundImageView my_message_userpic;
    private Button my_message_tuichu;
    private Context mContext;
    private String picPath=null;
    private SharedPreferences sp;

    UserListener userListener = new UserListener() {
        @Override
        public void onResponse(User userinfo) {
            my_message_username.setText(userinfo.getUsername());
            my_message_phone.setText(userinfo.getUserphone());
            Picasso.with(mContext).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/"+userinfo.getPic()).into(my_message_userpic);
        }

        @Override
        public void onFail() {

        }
    };

    SuccessListener successListener = new SuccessListener() {
        @Override
        public void onResponse(Success success) {
            if (success.getSuccess().equals("1")){
                Toast.makeText(MyMessageActivity.this,"更换成功", Toast.LENGTH_SHORT).show();
                finish();
            }else if (success.getSuccess().equals("0")){
                Toast.makeText(MyMessageActivity.this,"更换失败", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail() {

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_message);
        initView();
        initEvent();
        UserModel userModel = new UserModel();
        userModel.userinfo(sp.getString("uid",""),userListener);
    }

    private void initEvent() {

        my_message_back.setOnClickListener(this);
        my_message_replace.setOnClickListener(this);
        my_message_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(MyMessageActivity.this,MyEditPwdActivity.class);
                MyMessageActivity.this.startActivity(intent);
            }
        });

    }

    private void initView() {
        my_message_replace = (ImageView)findViewById(R.id.my_message_replace);
        my_message_username =(TextView)findViewById(R.id.my_message_username);
        my_message_phone =(TextView)findViewById(R.id.my_message_phone);
        my_message_userpic = (RoundImageView)findViewById(R.id.my_message_userpic);
        my_message_tuichu = (Button)findViewById(R.id.my_message_tuichu);
        my_message_back = (ImageView)findViewById(R.id.my_message_back);
        my_message_right = (ImageView)findViewById(R.id.my_message_right);
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.my_message_back:
                finish();
                break;

            case R.id.my_message_right:
                finish();
                break;
            case R.id.my_message_replace:
                final CharSequence[] items = {"相册"};
                new AlertDialog.Builder(MyMessageActivity.this).setTitle("选择获取图片方式").setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which==0){
                            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                            intent.setType("image/*");
                            startActivityForResult(intent,1);
                        }
                    }
                }).create().show();
                break;
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode== Activity.RESULT_OK){
            switch (requestCode){
                case 1:
                    if (data != null) {
                        Uri uri = data.getData();
                        my_message_userpic.setImageURI(uri);
                        ContentResolver cr = this.getContentResolver();
                        Cursor c = cr.query(uri, null, null, null, null);
                        c.moveToFirst();
                        //这是获取的图片保存在sdcard中的位置
                        picPath = c.getString(c.getColumnIndex("_data"));
                    } else {
                        return;
                    }
                    break;
            }
        }
    }

}
